package com.mis.pos.controller;

import com.alibaba.fastjson.JSON;
import com.mis.pos.common.utils.ApiResponse;
import com.mis.pos.entity.UserEntity;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.support.DefaultSubjectContext;
import org.apache.shiro.web.util.WebUtils;
import org.crazycake.shiro.RedisSessionDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.ServletRequest;

/**
 * @description: 管理路径控制器
 * @author: yuxiang
 * @create: 2019-12-12 20:34
 **/
@RestController
public class BaseController {

//    @Autowired
    private RedisSessionDAO redisSessionDAO = new RedisSessionDAO();

    public UserEntity getUserByHeader(ServletRequest request) throws Exception{
        //前端ajax的headers中必须传入Authorization的值
        String id = WebUtils.toHttp(request).getHeader("Authorization");
        Session session = redisSessionDAO.readSession(id);
        Object obj = session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
        SimplePrincipalCollection coll = (SimplePrincipalCollection) obj;
        String userStr = JSON.toJSON(coll.getPrimaryPrincipal()).toString();
        UserEntity user = JSON.parseObject(userStr, UserEntity.class);
        return user;
    }
}
